﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;

namespace CarLoanSystem
{
    public partial class frmLigigationInfo : Form
    {
        private static LitigationInfoItem LitigationItem;
        public frmLigigationInfo(int loanId, string unPaid)
        {
            InitializeComponent();
            LoanItem loanItem = DAL.LoanInfoDAL.LoadLoan(loanId);
            this.txtLoanId.Text = loanItem.LOAN_ID.ToString();
            this.txtPayMonth.Text = loanItem.PAY_MONTH.ToString();
            this.txtDown.Text = loanItem.DOWN_PAYMENT.ToString();
            this.txtRestMoney.Text = loanItem.DEBT_MONTH_REST.ToString();
            this.txtUnPaid.Text = unPaid;
            this.txtVehicleId.Text = loanItem.VEHICLE_ID;

            LitigationItem = DAL.LitigationInfoDAL.LoadLitigation(loanItem.LOAN_ID);

            CustomerItem cusItem = DAL.CustomerInfoDAL.LoadCustomer(loanItem.CUSTOMER_ID);
            this.txtName.Text = cusItem.CUSTOMER_NAME;
            this.txtLastName.Text = cusItem.CUSTOMER_LASTNAME;

            InterrestInfoItem InterrestItem = DAL.InterrestInfoDAL.LoadInterrest(loanItem.INTERREST_ID);
            this.txtInterrest.Text = InterrestItem.INTERREST_RATE.ToString() + "%";

            CarInfoItem carItem = DAL.CarInfoDAL.LoadCar(loanItem.CAR_ID);
            this.txtBrand.Text = carItem.CAR_BRAND_NAME;
            this.txtModel.Text = carItem.CAR_MODEL_NAME;
            this.txtPrice.Text = carItem.CAR_PRICE.ToString();

            if (LitigationItem.LITIGATION_STATUS == "A")
            {
                this.cbbStatusDebt.SelectedIndex = 0;
            }
            else if (LitigationItem.LITIGATION_STATUS == "B")
            {
                this.cbbStatusDebt.SelectedIndex = 1;
            }
            this.grvDetail.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.CellContentClick);
            genGridview();
        }

        private void genGridview()
        {
            // gen Grideview
            grvDetail.AutoGenerateColumns = false;

            //Set Columns Count
            grvDetail.ColumnCount = 4;

            grvDetail.Columns[0].Width = 45;

            //Add Columns
            grvDetail.Columns[1].Name = "LITIGATION_DETAIL_ID";
            grvDetail.Columns[1].HeaderText = "รหัส";
            grvDetail.Columns[1].DataPropertyName = "LITIGATION_DETAIL_ID";
            grvDetail.Columns[1].Width = 45;

            grvDetail.Columns[2].Name = "LITIGATION_DETAIL_DATE";
            grvDetail.Columns[2].HeaderText = "วันที่ติดต่อ";
            grvDetail.Columns[2].DataPropertyName = "LITIGATION_DETAIL_DATE";
            grvDetail.Columns[2].Width = 90;

            grvDetail.Columns[3].Name = "LITIGATION_DETAIL";
            grvDetail.Columns[3].HeaderText = "รายละเอียดการติดต่อ";
            grvDetail.Columns[3].DataPropertyName = "LITIGATION_DETAIL";
            grvDetail.Columns[3].Width = 190;
            // end gen gridview
            grvDetail.DataSource = LoadDeptDetail();
        }

        private void CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && ((DataGridView)sender).Columns[e.ColumnIndex].GetType() == typeof(DataGridViewButtonColumn))
            {
                frmLigigationInfoDetail frmLigigationInfoDetail = new frmLigigationInfoDetail(Convert.ToInt32(((DataGridView)sender).Rows[e.RowIndex].Cells[1].Value.ToString()), LitigationItem.LITIGATION_ID);
                frmLigigationInfoDetail.ShowDialog();
            }
        }

        private void btnAddContact_Click(object sender, EventArgs e)
        {
            frmLigigationInfoDetail frmLigigationInfoDetail = new frmLigigationInfoDetail(LitigationItem.LITIGATION_ID);
            frmLigigationInfoDetail.ShowDialog();
            grvDetail.DataSource = LoadDeptDetail();
        }

        private DataTable LoadDeptDetail()
        {
            DataTable dt = DAL.LitigationInfoDetailDAL.LoadAllLitigationDetail(LitigationItem.LITIGATION_ID);
            return dt;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cbbStatusDebt.SelectedItem.ToString().Equals("อยู่ระหว่างการติดต่อทางกฎหมาย"))
            {
                LitigationItem.LITIGATION_STATUS = "A";
            }
            else if (cbbStatusDebt.SelectedItem.ToString().Equals("ส่งเรื่องให้พนักงานทวงหนี้สินยึดรถ"))
            {
                LitigationItem.LITIGATION_STATUS = "B"; 
            }
            DAL.LitigationInfoDAL.UpdateLitigationStatus(LitigationItem);
            LoanItem loanItem = new LoanItem();
            loanItem.LOAN_ID = LitigationItem.LOAN_ID;
            if (LitigationItem.LITIGATION_STATUS == "A")
            {
                loanItem.DEBT_STATUS = "B";
            }
            else if (LitigationItem.LITIGATION_STATUS == "B")
            {
                loanItem.DEBT_STATUS = "C";
                DeptInfoItem deptInfoItem = DAL.DeptInfoDAL.LoadDept(loanItem.LOAN_ID);
                deptInfoItem.DEBT_STATUS = "C";
                DAL.DeptInfoDAL.UpdateDeptStatus(deptInfoItem);

                SequesterItem sequesterItem = new SequesterItem();
                sequesterItem.LOAN_ID = loanItem.LOAN_ID;
                sequesterItem.SEQUESTER_DATE = DateTime.Now;
                sequesterItem.SEQUESTER_STATUS = "A";
                DAL.SequesterInfoDAL.Insert(sequesterItem);

                EmployeeItem empItem = DAL.EmployeeInfoDAL.LoadEmployee(deptInfoItem.EMPLOYEE_ID.ToString());
                AlertsMessageItem alertItem = new AlertsMessageItem();
                alertItem.ALERTS_MESSAGE_DETAIL = "แจ้งเตือนการดำเนินเรื่องการยึดรถคืนของการขอกู้รหัส " + sequesterItem.LOAN_ID;
                alertItem.ALERTS_MESSAGE_STATUS = "A";
                alertItem.USER_NAME = empItem.USERNAME;
                alertItem.LOAN_ID = sequesterItem.LOAN_ID;
                DAL.AlertsMessageInfoDAL.Insert(alertItem);
            }
            DAL.LoanInfoDAL.UpdateDeptStatus(loanItem);
            MessageBox.Show("บันทึกข้อมูลสำเร็จ", "ข้อมูลการติดต่อทางกฎหมาย");
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
